//
//  ContentViewController.m
//  WZBlogMagazine
//
//  Created by Sunny on 11-11-22.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "ContentViewController.h"

#import "WZ66WZZBAppDelegate.h"
#import "MainViewController.h"
#import "MagazineViewController.h"
#import "UIWebViewController.h"

#import "UIDetailScrollView.h"

#import "Blog.h"

@implementation ContentViewController

@synthesize navigationBar;
@synthesize scrollView;
@synthesize activityIndicator;
@synthesize blogList;
@synthesize webViewControllerArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [activityIndicator release];
    activityIndicator = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return YES;
}

- (void)dataLoad {
	MainViewController *mainViewController = [WZ66WZZBAppDelegate sharedAppDelegate].mainViewController;
	if (self.blogList ==nil) {
        self.blogList = [[[NSMutableArray alloc] init] autorelease];
		for(int i=1;i <= [mainViewController.blogArray count];i++) {
			NSMutableArray * array = [mainViewController.blogArray objectForKey:[NSString stringWithFormat:@"%d",i]];
			for (Blog *blog in array) {
				[self.blogList addObject:blog];
			}
		}
	}
	
	int pageCount = self.blogList.count;
	
	scrollView.pagingEnabled = YES;
	scrollView.showsHorizontalScrollIndicator = NO;
	scrollView.showsVerticalScrollIndicator = NO;
	scrollView.backgroundColor=[UIColor grayColor];
	scrollView.scrollsToTop = NO;
	scrollView.delegate = self;
	
	scrollView.currentPage = 0;
    
    if (scrollView.hidden == YES) {
        scrollView.hidden = NO;
    }
    
    NSMutableArray *webViewcontrollers = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < 3; i++)
    {
        [webViewcontrollers addObject:[NSNull null]];
    }
    self.webViewControllerArray = webViewcontrollers;
    [webViewcontrollers release];
    //Add For reUse
    UIWebViewController *controller0 = [[UIWebViewController alloc] initWithPageNumber:0];
    UIWebViewController *controller1 = [[UIWebViewController alloc] initWithPageNumber:1];
    UIWebViewController *controller2 = [[UIWebViewController alloc] initWithPageNumber:2];
    [self.webViewControllerArray  replaceObjectAtIndex:0 withObject:controller0];
    [self.webViewControllerArray  replaceObjectAtIndex:1 withObject:controller1];
    [self.webViewControllerArray  replaceObjectAtIndex:2 withObject:controller2];
    
	[self loadScrollViewWithPage:0 scrollViewSender: scrollView];
	[self loadScrollViewWithPage:1 scrollViewSender: scrollView];
    
    [self.scrollView setContentOffset:CGPointMake(0,0)];
    self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * pageCount, 768);
}


- (void)loadScrollViewWithPage:(int)page scrollViewSender:(UIDetailScrollView *) scrollViewSender {
    if (page < 0)
		return;
	
	//MainViewController *mainViewController = [WZ66WZZBAppDelegate sharedAppDelegate].mainViewController;
	
	//DirectoryViewController *directoryViewController = [WZ66WZZBAppDelegate sharedAppDelegate].directoryViewController;
	
	if (page >= self.blogList.count)
		return;
	
	UIWebViewController *controller = [self.webViewControllerArray objectAtIndex:page%3];
	[controller.view removeFromSuperview];
	Blog *blog = [self.blogList objectAtIndex:page];
	[controller dataLoad:blog];
	
	// add the controller's view to the scroll view
	if (controller.view.superview == nil) {
		CGRect frame = scrollViewSender.frame;
		frame.origin.x = frame.size.width * page;
		frame.origin.y = 0;
		controller.view.frame = frame;
		
		[scrollViewSender addSubview:controller.view];
	}

}

- (void)scrollViewDidScroll:(UIDetailScrollView *)sender {
    // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
    // which a scroll event generated from the user hitting the page control triggers updates from
    // the delegate method. We use a boolean to disable the delegate logic when the page control is used.
    // Switch the indicator when more than 50% of the previous/next page is visible
	
    CGFloat pageWidth = sender.frame.size.width;
	
    int page = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
	
	// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
	if (sender.currentPage < page) {
		sender.currentPage = page;
		//[self loadScrollViewWithPage:page - 1 scrollViewSender: sender];
		//[self loadScrollViewWithPage:page scrollViewSender: sender];
		[self loadScrollViewWithPage:page + 1 scrollViewSender: sender];
	}
	else if(sender.currentPage > page) {
		sender.currentPage = page;
		[self loadScrollViewWithPage:page - 1 scrollViewSender: sender];
		//[self loadScrollViewWithPage:page scrollViewSender: sender];
		//[self loadScrollViewWithPage:page + 1 scrollViewSender: sender];
	}
	
	// A possible optimization would be to unload the views+controllers which are no longer visible
}

- (void)viewDetail:(int)page {
    self.scrollView.currentPage = page -1;
	[self loadScrollViewWithPage:page -1 scrollViewSender: self.scrollView];
	
	self.scrollView.currentPage = page;
	[self loadScrollViewWithPage:page scrollViewSender: self.scrollView];
	
	CGRect frame = scrollView.frame;
	frame.origin.x = frame.size.width * (page-1);
	frame.origin.y = 0;
	[scrollView scrollRectToVisible:frame animated:YES];
}

- (void)returnToMagazineList {
    self.blogList = nil;
    self.scrollView.hidden = YES;
    MainViewController *mainViewController = [WZ66WZZBAppDelegate sharedAppDelegate].mainViewController;
	[mainViewController dismissModalViewControllerAnimated:NO];
	MagazineViewController * magazineViewController = [WZ66WZZBAppDelegate sharedAppDelegate].magazineViewController;
	[mainViewController presentModalViewController:magazineViewController animated:NO];
}

- (void)returnToContent {
    [self viewDetail:1];
}

- (BOOL)isBarShow {
	return !(self.navigationBar.hidden);
}

- (void)showBar:(BOOL)animate {
    self.navigationBar.hidden = NO;
}

- (void)hiddenBar:(BOOL)animate {
    self.navigationBar.hidden = YES;
}

- (void)dealloc {
    [activityIndicator release];
    [super dealloc];
}
@end
